package view;

import util.Controller;
import util.MusicPlayer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

public class Sun extends JLabel implements Runnable{

    private int x;
    private int y;
    private int maxy;
    private int miny;
    private boolean isCollected = false;
    private int pic;
    private int allPic = 21;
    private int vy;
    private int vx;
    private Controller controller;
    Sun(Controller controller){
        this.x = (int) (Math.random()*660)+30;
        this.y = 40;
        this.vy = 1;
        this.vx = 0;
        this.miny = -999;
        this.maxy = (int) (Math.random()*420)+110;
        this.controller = controller;
        controller.getLayeredPane().add(this,new Integer(888));
    }
    Sun(Controller controller, int r, int c){
        this(controller);
        this.x = c * 80 + 35 + new Random().nextInt(5);
        this.y = r * 100 + 70;
        this.maxy = r * 100 + 105;
        this.miny = r * 100 + 55 + new Random().nextInt(5);
        this.vx =  2;
        this.vy = -1;
    }
    private Image image;   //你要绘制的图形
    private Image offScreenImage;  //图形缓存
    double alpha = 100;
    void picChange(){
        if (pic<allPic)
            pic++;
        else
            pic = 0;
    }
    void Gone(){
        for (int i=1 ; i<=60 ; i++){
            try {
                if (i%8==0){
                    picChange();
                }
                Thread.sleep(7);
                ReduceAlpha();
                repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        setVisible(false);
    }
    void ReduceAlpha(){
        alpha -=1.5;
    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        ImageIcon img = new ImageIcon("img\\Sun\\"+pic+".png");
        image = img.getImage();
        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(AlphaComposite.SrcOver.derive((float) alpha / 100F));
        g2.drawImage(image, 0, 0, img.getIconWidth(), img.getIconWidth(),  this);
    }
    public void update(Graphics g) {
        if(offScreenImage == null)
            offScreenImage = this.createImage(800, 600);
        Graphics gImage = offScreenImage.getGraphics();
        paint(gImage);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void run() {
        this.addMouseListener(new MouseAdapter() {
            Frame frame = controller.getFrame();
            @Override
            public void mouseClicked(MouseEvent e) {
                if (!isCollected){
                    //new Thread(new Collect(Sun.this, x, y)).start();
                    frame.setCursor(Frame.DEFAULT_CURSOR);
                    isCollected = true;
                }
            }
            @Override
            public void mousePressed(MouseEvent e) {
                if (!isCollected){
                    //new Thread(new Collect(Sun.this, x, y)).start();
                    frame.setCursor(Frame.DEFAULT_CURSOR);
                    isCollected = true;
                }
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                if (!isCollected){
                    frame.setCursor(Frame.HAND_CURSOR);
                }
            }
            @Override
            public void mouseExited(MouseEvent e) {
                if (!isCollected){
                    frame.setCursor(Frame.DEFAULT_CURSOR);
                }
            }
        });
        /*****                掉落                *****/
        for (int i=1 ; this.y<maxy&&!isCollected ; i++){
            if (this.y<=miny){
                vy = 2;
                vx = 1;
            }
            try {
                if (i%7==0){
                    this.picChange();
                }
                this.setBounds(x,y,78,78);
                Thread.sleep(8);
                if (i%2==0){
                    this.y += vy;
                    this.x += vx;
                }
                this.repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        /*****                落地                *****/
        for (int i=1 ; i<=900&&!isCollected ; i++){
            try {
                if (i%8==0){
                    this.picChange();
                }
                Thread.sleep(7);
                this.repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (isCollected){
            /*****                收集                *****/
            SunFactory sunFactory = controller.getSunFactory();
            JLabel sunCount = controller.getSunCount();
            sunFactory.setSumSun(sunFactory.getSumsun()+25);
            sunCount.setText(""+sunFactory.getSumsun());
            controller.checkCards();

            new Thread(new MusicPlayer("audio\\points.mp3")).start();
            for (int i=100 ; x>=33&&y>=35||y>=30&&x>=33 ; i++) {
                try {
                    if (i % 10 == 0) {
                        this.picChange();
                    }
                    if (x < y && y >= 100) {
                        y -= y / x * 1.5;
                    } else if (x > y && x >= 100) {
                        x -= x / y * 1.5;
                    }
                    x -= x / 100 + 1;
                    y -= y / 100 + 1;
                    this.repaint();
                    this.setBounds(x, y, 78, 78);
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        this.Gone();
    }
}
/*class Collect implements Runnable{
    private Sun sun;
    private int x;
    private int y;
    Collect(Sun sun, int x, int y){
        this.sun = sun;
        this.sun.alpha = 100;
        this.x = x;
        this.y = y;
    }
    @Override
    public void run() {
        new Thread(new MusicPlayer("audio\\points.mp3")).start();
        for (int i=100 ; x>=33&&y>=35||y>=30&&x>=33 ; i++){
            try {
                if (i%10==0){
                    sun.picChange();
                }
                if (x<y&&y>=100){
                    y -= y/x*1.5;
                }else if (x>y&&x>=100){
                    x -= x/y*1.5;
                }
                x -= x/100+1;
                y -= y/100+1;
                sun.repaint();
                sun.setBounds(x,y,78,78);
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        sun.Gone();
    }
}*/
